package com.jt.serviceImpl;

import com.jt.mapper.OrderItemMapper;
import com.jt.mapper.OrderMapper;
import com.jt.mapper.OrderShippingMapper;
import com.jt.pojo.Order;
import com.jt.pojo.OrderItem;
import com.jt.pojo.OrderShipping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author dc
 */
@Component
public class OrderServiceImpl{
	
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderShippingMapper orderShippingMapper;
	@Autowired
	private OrderItemMapper orderItemMapper;


	@Transactional(rollbackFor = RuntimeException.class)
	public void saveOrder(Order order) {
		Date date = order.getCreated();
		orderMapper.insert(order);
		System.out.println("入库订单成功!");
		//2.物流信息入库
		OrderShipping shipping = order.getOrderShipping();
		String orderId = order.getOrderId();
		shipping.setOrderId(orderId).setCreated(date).setUpdated(date);
		orderShippingMapper.insert(shipping);
		System.out.println("入库物流信息成功!");
		//3.订单商品入库
		List<OrderItem> orderItems = order.getOrderItems();
		for(OrderItem orderItem : orderItems){
			orderItem.setOrderId(orderId).setCreated(date).setUpdated(date);
			orderItemMapper.insert(orderItem);
		}
		System.out.println("订单入库完成");
	}
}
